/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.artic.ogre.handler;


import com.artic.base.xml.XMLUtil;
import com.artic.ogre.model.Light;
import com.artic.ogre.model.PointEnum;
import com.artic.ogre.model.Scene;
import com.jme3.asset.AssetManager;
import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import java.util.List;

/**
 *
 * @author czarnik_a
 */
public class LightHandler implements ISceneHandler{

    private ISceneHandler nextHandler;
    private AssetManager assetManager;
    private Scene scene;
    private Node node;

    public void addLight(Light light) {
        if (light.getType() == PointEnum.POINT) {
            PointLight pointLight = new PointLight();

            if (light.getColourSpecular() != null) {
                pointLight.setColor(new ColorRGBA(XMLUtil.getFloatString(light.getColourSpecular().getR()), XMLUtil.getFloatString(light.getColourSpecular().getG()), XMLUtil.getFloatString(light.getColourSpecular().getB()), 0.0f));
            }
            node.addLight(pointLight);
        }

        if (light.getType() == PointEnum.DIRECTIONAL) {
            DirectionalLight directionlLight = new DirectionalLight();
            if (light.getColourSpecular() != null) {
                directionlLight.setColor(new ColorRGBA(XMLUtil.getFloatString(light.getColourSpecular().getR()), XMLUtil.getFloatString(light.getColourSpecular().getG()), XMLUtil.getFloatString(light.getColourSpecular().getB()), 0.0f));
            }
            
            if (light.getDirection() != null) {
                directionlLight.setDirection(new Vector3f(XMLUtil.getFloatString(light.getDirection().getX()), XMLUtil.getFloatString(light.getDirection().getY()), XMLUtil.getFloatString(light.getDirection().getZ())));
            }
            node.addLight(directionlLight);
        }

    }

    public void setLights(List<Light> lightList) {
        for (Light light : lightList) {
            addLight(light);
        }
    }

    public void setNext(ISceneHandler handler) {
        nextHandler = handler;
    }

    public void handleScene(Scene scene, Node node, AssetManager assetManager) {
        this.assetManager = assetManager;
        this.scene = scene;
        this.node = node;
        buildLights();
        if(nextHandler != null) nextHandler.handleScene(scene, node, assetManager);
    }

    public void buildLights(){
        for (Light light : scene.getLight()) {
            addLight(light);
        }
    }
    public AssetManager getAssetManager() {
        return assetManager;
    }

    public void setAssetManager(AssetManager assetManager) {
        this.assetManager = assetManager;
    }

    public Node getNode() {
        return node;
    }

    public void setNode(Node node) {
        this.node = node;
    }

    public Scene getScene() {
        return scene;
    }

    public void setScene(Scene scene) {
        this.scene = scene;
    }

}
